import { Model, DataTypes, Optional } from 'sequelize';
import { sequelize } from '../../config/database';

// 房间接口
interface RoomAttributes {
  id: string;
  title?: string | null;
  coverUrl?: string | null;
  streamerName?: string | null;
  streamerAvatar?: string | null;
  streamerDisplayId?: string | null;
  streamerShortId?: string | null;
  followCount?: number | null;
  type?: string | null;
  weight?: number | null;
  createdAt?: Date;
  updatedAt?: Date;
}

// 创建时可选的属性
interface RoomCreationAttributes extends Optional<RoomAttributes, 'createdAt' | 'updatedAt'> {}

// 房间模型类
class Room extends Model<RoomAttributes, RoomCreationAttributes> implements RoomAttributes {
  declare id: string;
  declare title: string | null;
  declare coverUrl: string | null;
  declare streamerName: string | null;
  declare streamerAvatar: string | null;
  declare streamerDisplayId: string | null;
  declare streamerShortId: string | null;
  declare followCount: number | null;
  declare type: string | null;
  declare weight: number | null;
  
  // 时间戳
  declare readonly createdAt: Date;
  declare readonly updatedAt: Date;
}

// 初始化模型
Room.init(
  {
    id: {
      type: DataTypes.STRING,
      primaryKey: true,
      allowNull: false
    },
    title: {
      type: DataTypes.STRING,
      allowNull: true
    },
    coverUrl: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    streamerName: {
      type: DataTypes.STRING,
      allowNull: true
    },
    streamerAvatar: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    streamerDisplayId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    streamerShortId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    followCount: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    type: {
      type: DataTypes.STRING,
      allowNull: true
    },
    weight: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    }
  },
  {
    sequelize,
    tableName: 'rooms',
    modelName: 'Room',
    timestamps: true
  }
);

export default Room;